package com.binsoft.ndtta;

import com.binsoft.ndtta.R;

import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class MatchDialogFragment extends DialogFragment implements OnClickListener
{
	private String mHomeTeam = "";
	private String mAwayTeam = "";
	private String mHomeScore = "";
	private String mAwayScore = "";
	private String mDate = "";
	private int mWeek = 0;
	private int mTeam = 0;
	
	TextView mHomeScoreField = null;
	TextView mAwayScoreField = null;
	
    static MatchDialogFragment newInstance(String date, int week, int team, String homeTeam, String awayTeam, int homeScore, int awayScore ) 
    {
    	MatchDialogFragment f = new MatchDialogFragment();

        Bundle args = new Bundle();
        args.putString("homeTeam", homeTeam);
        args.putString("awayTeam", awayTeam);
        args.putString("homeScore", ""+homeScore);
        args.putString("awayScore", ""+awayScore);
        args.putString("date", date);
        args.putInt("week", week );
        args.putInt("team", team );
        
        f.setArguments(args);

        return f;
    }

    @Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		Bundle args = getArguments();
		
		mHomeTeam = args.getString("homeTeam");
		mAwayTeam = args.getString("awayTeam");
		mHomeScore = args.getString("homeScore");
		mAwayScore = args.getString("awayScore");
		mDate = args.getString("date");
		mWeek = args.getInt("week");
		mTeam = args.getInt("team");
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState)
	{
	    View v = inflater.inflate(R.layout.match_layout, container, false);
	
		TextView matchHome = (TextView)v.findViewById( R.id.matchHomeTeam );
		TextView matchAway = (TextView)v.findViewById( R.id.matchAwayTeam );
		mHomeScoreField = (TextView)v.findViewById( R.id.matchHomeScore );
		mAwayScoreField = (TextView)v.findViewById( R.id.matchAwayScore );
		TextView date = (TextView)v.findViewById( R.id.matchDateField );
		// Get the button and add a listener
		Button okButton = (Button)v.findViewById( R.id.matchOKButton );			
		okButton.setOnClickListener( this );

		matchHome.setText( mHomeTeam );
		matchAway.setText( mAwayTeam );
		mHomeScoreField.setText( mHomeScore );
		mAwayScoreField.setText( mAwayScore );
		date.setText( mDate );
				
		getDialog().setTitle("Match Score");

		return v;
	}

	@Override
	public void onClick(View v)
	{
		((MainActivity)getActivity()).updateMatch(mWeek, mTeam, mHomeScoreField.getText().toString(),mAwayScoreField.getText().toString());
		
		dismiss();
	}

}
